import { PartialType } from '@nestjs/mapped-types';
import { Transform } from 'class-transformer';
import { IsBoolean, IsNotEmpty, IsNumber, IsOptional, MaxLength } from 'class-validator';

export class CreateCartDto {
  @IsOptional()
  @IsNumber()
  @Transform(({ value }) => parseInt(value))
  userId?: number;

  @IsNotEmpty({ message: '商品id不得为空' })
  @IsNumber()
  @Transform(({ value }) => parseInt(value))
  productId: number;

  @IsOptional()
  @MaxLength(255, { message: '商品名称最长为255' })
  productName?: string;

  @IsOptional()
  image?: string;

  @IsOptional()
  @Transform(({ value }) => parseFloat(value))
  price?: number;

  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  quantity: number;

  @IsOptional()
  @IsBoolean()
  selected?: boolean;
}

export class UpdateCartDto extends PartialType(CreateCartDto) {
  @IsNotEmpty({ message: 'id不得为空' })
  @IsNumber()
  @Transform(({ value }) => parseInt(value))
  id: number;
}
